<?php
class AdminController extends Controller {

	protected $tableName;
	protected $model;
	protected $map = array();

	public function __construct() {

		C('tpl_theme', '');
		C('layout', true);
		C('rewritepower', 0);
		C('layout_name', '/application/admin/view/public_layout.html');

		$this->skipnode = array('admin.index.index');
		$this->skinAction = array('ajax', 'uploadone');
	}

	public function init() {

		$this->session->start();
		if (empty($_SESSION['admin']['username'])) {
			$this->redirect(U('admin.public.login'));
		} else {
			$this->username = $_SESSION['admin']['username'];
			$this->groupname = $_SESSION['admin']['groupname'];
			session_write_close();
		}
		if ($this->tableName) {
			$this->model = M($this->tableName);}
		$this->menuinfo = M('admin_node')->getMenuInfo();

		if ($_SESSION['admin']['userid'] != '1' && !in_array($this->menuinfo['nodeid'], explode(',', dc::get('admin_group', $_SESSION['admin']['groupid'], 'node'))) && !in_array(MODULE_NAME . '.' . CONTROLLER_NAME . '.' . ACTION_NAME, $this->skipnode) && in_array(ACTION_NAME, $this->skinAction)) {

			$this->error('您没有权限访问这个页面！', 0, 0);
		}
		$this->response->disableRender();
	}
	public function delAction() {

		$zym_15 = I('request.id', 'int', 0);
		$this->model->del(array('id' => $zym_15));
		$this->success('删除成功');
	}
	public function multiAction() {

		$zym_16['update_user_id'] = $_SESSION['admin']['userid'];
		$zym_16['update_time'] = NOW_TIME;

		if (isset($_POST['changestatus'])) {
			foreach ($_POST['id'] as $zym_14 => $zym_18) {
				$zym_16['id'] = $zym_18;
				$zym_16['status'] = $_POST['value'][$zym_14];
				$this->model->edit($zym_16);
			}
			$this->success('修改状态成功');
		} elseif (isset($_POST['changeisover'])) {
			foreach ($_POST['id'] as $zym_14 => $zym_18) {
				$zym_16['id'] = $zym_18;
				$zym_16['isover'] = $_POST['value'][$zym_14];
				$this->model->edit($zym_16);
			}
			$this->success('修改状态成功');

		} elseif (isset($_POST['method'])) {
			switch ($_POST['method']) {
			case 'reorder':
				foreach ($_POST['id'] as $zym_15) {
					$zym_16['id'] = $zym_15;
					$zym_16['ordernum'] = $_POST['ordernum'][$zym_15];
					$this->model->edit($zym_16);
				}
				break;
			case 'rebuild':
				foreach ($_POST['id'] as $zym_15) {
					$this->model->createJs($zym_15);
				}
				break;
			case 'mdel':
				foreach ($_POST['id'] as $zym_15) {
					$this->model->del(array('id' => $zym_15));
				}
				break;case 'recover':foreach ($_POST['id'] as $zym_15) {
					$zym_16['id'] = $zym_15;
					$zym_16['status'] = 1;
					$this->model->edit($zym_16);
				}
				break;
			case 'forbidden':
				foreach ($_POST['id'] as $zym_15) {
					$zym_16['id'] = $zym_15;
					$zym_16['status'] = 0;
					$this->model->edit($zym_16);
				}
				break;
			default:$this->error('未定义指定的操作');}
			$this->success('操作成功');
		} else {
			var_dump($_POST);
		}
	}

	public function ajaxAction() {
		$zym_15 = I('request.id', 'int', 0);
		$zym_12 = I('param', 'en', '');

		$zym_13 = $this->model->where(array('key' => $zym_12))->field('id')->getField('id');
		if ($zym_13 && $zym_13 != $zym_15) {
			$zym_10 = array('status' => 'n', 'info' => '您输入的Key已经使用过了');
		} else {
			$zym_10 = array('status' => 'y', 'info' => '您输入的Key可以使用');}$this->ajax($zym_10);
	}

	protected function _parsemap() {

		$zym_6 = I("searchtype", 'str', '');
		$zym_5 = I("searchkey", 'str', '');

		if ($zym_5 && $zym_6) {
			return array($zym_6 => array('like', '%' . $zym_5 . '%'));
		}
		return array();
	}
	public function uploadoneAction() {
		$zym_7 = new upload();

		if (!empty($_FILES)) {

			$zym_17 = array_shift($_FILES);

			if (!empty($_POST['original_filename'])) {
				$zym_17['name'] = $_POST['original_filename'];
			}

			$zym_11 = md5(F($zym_17['tmp_name']));

			$zym_8 = M('attachment')->where(array('hash' => $zym_11))->getfield('url');

			if ($zym_8) {
				$this->ajax(array("success" => true, 'msg' => 'success', 'file_path' => $zym_8));
			}

			C('storage_path', C('upload_path', null, 'uploads'));

			$zym_7->setFile($zym_17);
			$zym_10 = $zym_7->uploadone();

			if ($zym_10['status'] == 1) {

				$zym_16 = array(
					'create_user_id' => $_SESSION['admin']['userid'],
					'create_time' => NOW_TIME,
					'name' => $zym_10['info']['filename'],
					'path' => $zym_10['info']['filepath'],
					'url' => $zym_10['info']['fileurl'],
					'ext' => $zym_10['info']['ext'],
					'size' => $zym_10['info']['size'],
					'hash' => $zym_10['info']['hash'],
				);

				M('attachment')->add($zym_16);
				$zym_9 = array("success" => true, 'msg' => 'success', 'file_path' => $zym_10['info']['fileurl']);
			} else {
				$zym_9 = array("success" => false, 'msg' => $zym_10['info']);
			}
		} else {
			$zym_9 = array("success" => false, 'msg' => '没有找到上传的文件');
		}
		$this->ajax($zym_9);
	}
}
?>